import Vue from 'vue'
import Router from 'vue-router'
import Login from "../views/Login";
import Admin from '../views/Admin'
import AddArt from '../components/article/AddArt'
import ArtList from '../components/article/ArtList'
import CategoryList from '../components/category/CategoryList'
import UserList from '../components/user/UserList'
import Upload from '../components/user/Upload'
import EditArticle from '../components/article/EditArticle'
Vue.use(Router)

const r = new Router({
  mode: "history",
  routes: [
    {
      path: '/upload',
      name: 'upload',
      component: Upload
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/admin',
      name: 'admin',
      component: Admin,
      children: [
        {path:'addArt', name: 'addArt', component: AddArt},
        {path: 'artList', name: 'artList', component: ArtList},
        {path: 'categoryList', name: 'categoryList', component: CategoryList},
        {path: 'userList', name: 'userList', component: UserList},
        {path: '/EditArticle', name: 'EditArticle', component: EditArticle},
      ]
    },
  ]
})
r.beforeEach((to, from, next) => {
  const token = window.sessionStorage.getItem('token')
  if (to.path === 'login') return next()
  if(!token && to.path ==='/admin'){
    next('/admin/index')
  } else{
    next()
  }
})
export default r
